package com.project.service.impl;

import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl extends BaseService implements IUserService {
    @Override
    public void addUser(UserBean user, int[] gradeArray) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.addUser(user);
        mapper.addGrade(user.getId(), gradeArray);

        session.commit();
        session.close();
    }

    @Override
    public void delUser(int id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.delUser(id);
        session.commit();
        session.close();
    }

    @Override
    public void updateGrade(int userId, int[] gradeArray) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.updateGrade(userId, gradeArray);
        session.commit();
        session.close();
    }

    @Override
    public UserBean findByIdIncludeGrade(int id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findByIdIncludeGrade(id);
        session.close();
        return user;
    }

    public static void main(String[] args) {
        IUserService service = new UserServiceImpl();
//        service.addUser(new UserBean("rose"), new int[]{1,2,3});
//        service.delUser(4);
//        service.updateGrade(3, new int[]{2,7,6,8});
        System.out.println(service.findByIdIncludeGrade(1));
    }
}
